<?php

namespace App\Models\Game\Platform\Org;

use App\Models\Game\Model;
use App\Models\Traits\Game\LogsActivity;
use Illuminate\Database\Eloquent\SoftDeletes;
use phpseclib3\Crypt\RSA;

class Wallet extends Model
{
    use LogsActivity, SoftDeletes;

    /**
     * 数据表名称
     *
     * @var string
     */
    protected $table = 't_third_wallet';

    /**
     * 【活动日志】忽略日志记录中的属性
     *
     * @var array
     */
    protected static $logAttributesToIgnore = ['private_key'];

    /**
     * 查询后隐藏的字段
     *
     * @var array
     */
    protected $hidden = [
        'created',
        'updated',
        'private_key',
    ];

    /**
     * 不可批量赋值的字段
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * 进行类型转换的字段
     *
     * @var array
     */
    protected $casts = [
        'deleted' => 'timestamp',
        'created' => 'timestamp',
        'updated' => 'timestamp',
        'config' => 'array',
    ];

    /**
     * 获取 AccessKey
     *
     * @param string $platCode
     * @return array
     */
    protected function getAccessKey($platCode): array
    {
        $data = AccessKey::withTrashed()->where('plat_code', $platCode)->first();

        return is_null($data) ? [] : $data->only('access_key_id', 'access_key_secret');
    }

    /**
     * 制作 RSA 密钥
     *
     * @return array
     */
    protected function makeRsaKey(): array
    {
        return [
            'private_key' => (string)$private = RSA::createKey(),
            'public_key' => (string)$private->getPublicKey(),
        ];
    }

    /**
     * 制作密钥
     *
     * @return array
     */
    protected function makeKey($platCode): array
    {
        return $this->makeRsaKey() + $this->getAccessKey($platCode);
    }
}
